/*
 * Copyright (c) 2011, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the Jombi project nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL JOMBI BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.miblounge.jombi.impl.widgets;

import org.jowidgets.common.types.Position;
import org.jowidgets.common.widgets.controller.IActionListener;
import org.jowidgets.common.widgets.controller.IPopupDetectionListener;
import org.jowidgets.spi.impl.controller.ActionObservable;
import org.jowidgets.spi.impl.controller.PopupDetectionObservable;
import org.jowidgets.spi.widgets.IToolBarPopupButtonSpi;

import com.trolltech.qt.core.Qt.WidgetAttribute;
import com.trolltech.qt.gui.QAction;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QMenu;
import com.trolltech.qt.gui.QToolButton;
import com.trolltech.qt.gui.QToolButton.ToolButtonPopupMode;

public class ToolBarPopupButtonImpl extends ToolBarItemImpl implements IToolBarPopupButtonSpi {

	private final PopupDetectionObservable popupDetectionObservable;
	private final ActionObservable actionObservable;

	//private final QMenu menu = null;

	public ToolBarPopupButtonImpl(final QToolButton item) {
		super(item);
		this.actionObservable = new ActionObservable();
		this.popupDetectionObservable = new PopupDetectionObservable();

		getUiReference().setMenu(createEmptyMenu());

		final QAction defaultAction = new QAction(getUiReference());
		defaultAction.triggered.connect(this, "fireActionPerformed()");

		getUiReference().setPopupMode(ToolButtonPopupMode.MenuButtonPopup);
		getUiReference().setDefaultAction(defaultAction);

		//item.customContextMenuRequested.connect(this, "fireMenu()");
		//item.setArrowType(ArrowType.DownArrow);
		//item.setPopupMode(ToolButtonPopupMode.MenuButtonPopup);

		//		item.addListener(SWT.Selection, new Listener() {
		//			@Override
		//			public void handleEvent(final Event event) {
		//				if (event.detail == SWT.ARROW) {
		//					final Rectangle bounds = item.getBounds();
		//					final Point point = new Point(bounds.x, bounds.y + bounds.height);
		//					popupDetectionObservable.firePopupDetected(new Position(point.x, point.y));
		//				}
		//			}
		//		});

	}

	protected QMenu createEmptyMenu() {
		final QMenu result = new QMenu();
		getUiReference().setMenu(result);
		result.aboutToShow.connect(this, "fireMenu()");
		result.addAction("dummy");
		result.setAttribute(WidgetAttribute.WA_DontShowOnScreen);
		return result;
	}

	protected void fireMenu() {
		final Position position = new Position(getUiReference().pos().x(), getUiReference().height());
		popupDetectionObservable.firePopupDetected(position);

		// Start thread to close the unwanted menu
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(15);
				} catch (final InterruptedException e) {
					// ignore exception, no handling necessary
					return;
				} finally {
					QApplication.invokeAndWait(new Runnable() {
						@Override
						public void run() {
							getUiReference().menu().close();
						}
					});
				}
			}
		}).start();
	}

	@Override
	public void addPopupDetectionListener(final IPopupDetectionListener listener) {
		popupDetectionObservable.addPopupDetectionListener(listener);
	}

	@Override
	public void removePopupDetectionListener(final IPopupDetectionListener listener) {
		popupDetectionObservable.removePopupDetectionListener(listener);
	}

	protected void fireActionPerformed() {
		actionObservable.fireActionPerformed();
	}

	@Override
	public void addActionListener(final IActionListener actionListener) {
		actionObservable.addActionListener(actionListener);
	}

	@Override
	public void removeActionListener(final IActionListener actionListener) {
		actionObservable.removeActionListener(actionListener);
	}

}
